package com.ruoyi.web.controller.system;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.dto.StatisGroupByYewLevel;
import com.ruoyi.web.dto.WcSysUserEx;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WcSysUser;
import com.ruoyi.system.service.IWcSysUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小程序端用户Controller
 * 
 * @author ruoyi
 * @date 2020-01-20
 */
@Controller
@RequestMapping("/system/wcuser")
public class WcSysUserController extends BaseController
{
    private String prefix = "system/wcuser";

    @Autowired
    private IWcSysUserService wcSysUserService;



    @RequiresPermissions("system:wcuser:view")
    @GetMapping()
    public String wcuser()
    {
        return prefix + "/wcuser";
    }

    Map<String, Object> list2Map(List<StatisGroupByYewLevel> list) {
        Map<String, Object> map = new HashMap<>();
        int all = 0;
        for (StatisGroupByYewLevel statisGroupByYewLevel: list
        ) {
            all += statisGroupByYewLevel.getAmount();

            if(statisGroupByYewLevel.getYewLevel().equals("29")) {
                map.put("cheyS", statisGroupByYewLevel.getAmount());
            }
            if(statisGroupByYewLevel.getYewLevel().equals("30")) {
                map.put("chuangkS", statisGroupByYewLevel.getAmount());
            }
            if(statisGroupByYewLevel.getYewLevel().equals("31")) {
                map.put("chesS", statisGroupByYewLevel.getAmount());
            }
            if(statisGroupByYewLevel.getYewLevel().equals("32")) {
                map.put("yunyS", statisGroupByYewLevel.getAmount());
            }
        }

        map.put("all", all);

        return map;
    }

    /**
     * 查询小程序端用户列表
     */
    @RequiresPermissions("system:wcuser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WcSysUser wcSysUser)
    {
        startPage();
        List<WcSysUser> list = wcSysUserService.selectWcSysUserList(wcSysUser);


        List<WcSysUserEx> list2 = new ArrayList<>();
        for (WcSysUser wcSysUser1: list
             ) {
            WcSysUserEx wcSysUserEx = new WcSysUserEx();
            BeanUtils.copyProperties(wcSysUser1, wcSysUserEx);

            List<StatisGroupByYewLevel> list3 = wcSysUserService.statisticsUnderling(wcSysUser1.getId());

            Map<String, Object>  map = list2Map(list3);
            wcSysUserEx.setCheyS(map.get("cheyS") == null ? "0" : map.get("cheyS") + "");
            wcSysUserEx.setChuangkS(map.get("chuangkS") == null ? "0" : map.get("chuangkS") + "");
            wcSysUserEx.setChesS(map.get("chesS") == null ? "0" : map.get("chesS") + "");
            wcSysUserEx.setYunyS(map.get("yunyS") == null ? "0" : map.get("yunyS") + "");

            wcSysUserEx.setAll(map.get("all") + "");


            list2.add(wcSysUserEx);
        }

        long total = wcSysUserService.countSysUser();
        return getDataTable(list2, total);
    }

    /**
     * 导出小程序端用户列表
     */
    @RequiresPermissions("system:wcuser:export")
    @Log(title = "小程序端用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WcSysUser wcSysUser)
    {
        List<WcSysUser> list = wcSysUserService.selectWcSysUserList(wcSysUser);
        ExcelUtil<WcSysUser> util = new ExcelUtil<WcSysUser>(WcSysUser.class);
        return util.exportExcel(list, "wcuser");
    }

    // 导出单条记录
    @GetMapping("/export4single")
    @ResponseBody
    public AjaxResult export4single(Integer wcSysUserId)
    {
        WcSysUser wcSysUser = wcSysUserService.selectWcSysUserById(wcSysUserId);

//        ExcelUtil<WcSysUser> util = new ExcelUtil<WcSysUser>(WcSysUser.class);
//        return util.exportExcel(list, "wcuser");
        return null;
    }

    /**
     * 新增小程序端用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存小程序端用户
     */
    @RequiresPermissions("system:wcuser:add")
    @Log(title = "小程序端用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WcSysUser wcSysUser)
    {
        return toAjax(wcSysUserService.insertWcSysUser(wcSysUser));
    }

    /**
     * 修改小程序端用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        WcSysUser wcSysUser = wcSysUserService.selectWcSysUserById(id);
        mmap.put("wcSysUser", wcSysUser);

        List<WcSysUserEx> list = new ArrayList();
        List<WcSysUser> list2 = wcSysUserService.selectWcSysUserList(null);
        for (WcSysUser wcSysUser1: list2 ) {
            WcSysUserEx wcSysUserEx = new WcSysUserEx();
            wcSysUserEx.setId(wcSysUser1.getId());
             wcSysUserEx.setNameAndMobile(wcSysUser1.getRealName() + " ( " + wcSysUser1.getMobile() + " )");
            list.add(wcSysUserEx);
        }
        mmap.put("userList", list);

        return prefix + "/edit";
    }

    /**
     * 修改保存小程序端用户
     */
    @RequiresPermissions("system:wcuser:edit")
    @Log(title = "小程序端用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WcSysUser wcSysUser)
    {
        System.err.println(wcSysUser.toString());
        int rtn = wcSysUserService.updateWcSysUser(wcSysUser);
        return toAjax(rtn);
    }

    /**
     * 删除小程序端用户
     */
    @RequiresPermissions("system:wcuser:remove")
    @Log(title = "小程序端用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wcSysUserService.deleteWcSysUserByIds(ids));
    }
}
